package com.yinhe.music.yhmusic.singer;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.SingerAdapter;
import com.yinhe.music.yhmusic.adapter.SingerAdapter.SingerAdapterListener;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.model.DataItem;
import com.yinhe.music.yhmusic.model.SingerList;
import com.yinhe.music.yhmusic.model.Tags;
import com.yinhe.music.yhmusic.singer.info.SingerInfoActivity;
import com.yinhe.music.yhmusic.singer.list.ISingerListContract;
import com.yinhe.music.yhmusic.singer.list.SingerListPresenter;
import com.yinhe.music.yhmusic.singer.parse.Singer;
import com.yinhe.music.yhmusic.singer.parse.SingerGroup;
import com.yinhe.music.yhmusic.singer.parse.SingerParseUtil;
import com.yinhe.music.yhmusic.utils.Preferences;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Johnson
 * @date 2018/2/10
 */

public class SingerActivity extends BaseServiceActivity implements SingerAdapterListener,
        SlideBar.OnTouchAssortListener, ISingerListContract.ISingerListView {

    private ListView mSingerView;
    private SingerAdapter mSingerAdapter;
    private SlideBar mSlideBar;

    private LinearLayout mTagLayout;

    private View mEmptyView;
    private int mTagType = -1;

    private List<SingerGroup> mSingerGroups;

    SingerListPresenter mPresenter;

    @Override
    public void initView() {
        mTagLayout = findViewById(R.id.tag_layout);
        mSingerView = findViewById(R.id.singer_view);
        mSlideBar = findViewById(R.id.slidebar);
        mSlideBar.setOnTouchAssortListener(this);
        mSlideBar.setAllType(true);

        mEmptyView = findViewById(R.id.empty_layout);
        mEmptyView.setVisibility(View.GONE);

        showQuickControl(true);
        setToolbar(getString(R.string.singer));

        mSingerAdapter = new SingerAdapter(this, this);
        mSingerView.setFocusable(false);
        mSingerView.setAdapter(mSingerAdapter);

        initTagInfo();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.singer_activity;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SingerListPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    /**
     * 歌手分类
     */
    private void initTagInfo() {
        List<Tags> tagsList = new ArrayList<>();
        tagsList.add(new Tags("全部", 0));
        tagsList.add(new Tags("男", 1));
        tagsList.add(new Tags("女", 2));
        tagsList.add(new Tags("组合", 3));
        Tags tags = new Tags(tagsList);
        initTagUI(tags.getTags());
    }

    /**
     * 请求歌手列表
     *
     * @param type 歌手分类
     */
    private void requestData(int type) {
        if (mSingerGroups == null) {
            mTagType = type;
            mPresenter.getSingerList();
        } else {
            List<DataItem> datas = SingerParseUtil.getSingerByType(type, mSingerGroups);
            if (datas != null) {
                mSingerAdapter.updateData(datas);
                if (!datas.isEmpty()) {
                    mEmptyView.setVisibility(View.INVISIBLE);
                } else {
                    mEmptyView.setVisibility(View.VISIBLE);
                }
            }
            mSlideBar.setAllType(type == 0);
        }
    }

    /**
     * 初始化布局
     *
     * @param mTagsList 歌手分类
     */
    private void initTagUI(List<Tags> mTagsList) {
        mTagLayout.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(this);
        View first = null;
        for (final Tags tag : mTagsList) {
            TextView view = (TextView) inflater.inflate(R.layout.singer_tag_layout, mTagLayout, false);
            view.setText(tag.getName());
            view.setTag(tag);
            view.setOnClickListener(view1 -> setComposeViewSelected(view1));
            mTagLayout.addView(view);
            if (first == null) {
                first = view;
            }
        }
        if (first != null) {
            setComposeViewSelected(first);
        }
    }

    /**
     * 解析歌手列表数据
     *
     * @param singerList 歌手列表数据
     * @param type       歌手分类
     */
    private void initSingeData(SingerList singerList, int type) {
        List<SingerList> frequentlistern = singerList.getRecentListenList();

        if (frequentlistern != null) {
            mSingerAdapter.mFreSingerList.clear();
            mSingerAdapter.mFreSingerList.addAll(frequentlistern);
        }

        List<SingerList> hotSingers = singerList.getHotSingerList();

        if (hotSingers != null) {
            mSingerAdapter.mHotSingerList.clear();
            mSingerAdapter.mHotSingerList.addAll(hotSingers);
        }

        String singers = singerList.getSingerListStr();

        if (singers != null) {
            mSingerGroups = SingerParseUtil.parseSingerData(singers);
            if (mSingerGroups != null) {
                List<DataItem> datas = SingerParseUtil.getSingerByType(type, mSingerGroups);
                if (datas != null) {
                    mSlideBar.setAllType(type == 0);
                    mSingerAdapter.updateData(datas);
                    if (!datas.isEmpty()) {
                        mEmptyView.setVisibility(View.INVISIBLE);
                    } else {
                        mEmptyView.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    }

    /**
     * 设置选中的分类
     *
     * @param view TextView
     */
    private void setComposeViewSelected(View view) {
        if (view.isSelected()) {
            return;
        }

        for (int i = 0; i < mTagLayout.getChildCount(); i++) {
            View aview = mTagLayout.getChildAt(i);
            aview.setSelected(false);
        }

        view.setSelected(true);
        Tags tag = (Tags) view.getTag();
        if (tag != null) {
            mTagType = tag.getType();
            mSingerAdapter.notifyDataSetChanged();
            requestData(tag.getType());
        }
    }

    @Override
    public void singerDidClick(Singer singer) {
        if (singer != null) {
            Intent intent = new Intent(SingerActivity.this, SingerInfoActivity.class);
            intent.putExtra("singerId", Integer.parseInt(singer.id));
            startActivity(intent);
        }
    }

    @Override
    public void onTouchAssortListener(String s) {
        int select = mSingerAdapter.getSelectIndex(s);
        if (select != -1) {
            mSingerView.setSelection(select);
        }
    }

    @Override
    public void setSingerListUI(SingerList singerList) {
        Preferences.setImageHost(singerList.getImageHost());
        initSingeData(singerList, mTagType);
    }

    @Override
    public void setLoadMoreFailUI() {

    }
}
